<?php

class System_Menu {

    /**
     * Call this main function only.
     * @param type $visible => visibled, all
     * @return array
     */
    public function getMenuArray($visible) {
        $parentArray = array();
        $menuDb = new Admin_Model_Menu();
        $parents = $menuDb->getParents(null);
        if (sizeof($parents) > 0) {
            foreach ($parents as $parent) {
                $localArray = array();
                switch ($visible) {
                    case "visibled":
                        if ($parent->visible) {
                            $localArray = $this->doForParent($parent, $visible);
                            array_push($parentArray, $localArray);
                        }
                        break;
                    case "disabled":
                        break;

                    case "all":
                        $localArray = $this->doForParent($parent, $visible);
                        array_push($parentArray, $localArray);
                        break;
                }
            }
        }
        return $parentArray;
    }

    /**
     * child of getMenuArray
     * @param type $parent
     * @param type $visible
     * @return type
     */
    public function doForParent($parent, $visible) {
        $localArray = array();
        $parentArray = array();
        $parentArray['id'] = $parent->id;
        $parentArray['name'] = $parent->name;
        $parentArray['visible'] = $parent->visible;
        #child start       
        $childArray = $this->getChildArray($parent->id, $visible);
        #child over                                                
        $localArray['parent'] = $parentArray;
        $localArray['child'] = $childArray;
        return $localArray;
    }

    /**
     * child of doForParent
     * @param type $parent_id
     * @param type $visible
     * @return array
     */
    public function getChildArray($parent_id, $visible) {

        $localMenu = array();
        $menuDb = new Admin_Model_Menu();
        $childs = $menuDb->getChilds($parent_id, null);
        if (sizeof($childs) > 0) {
            foreach ($childs as $child) {
                switch ($visible) {
                    case "visibled":
                        if ($child->visible) {
                            array_push($localMenu, $this->doForChild($child));
                        }
                        break;
                    case "all":

                        array_push($localMenu, $this->doForChild($child));
                        break;
                }
            }
        }
        return $localMenu;
    }

    /**
     * child of GetChildArray
     * @param type $child
     * @return type
     */
    public function doForChild($child) {
        $childArray = array();
        $childArray['id'] = $child->id;
        $childArray['name'] = $child->name;
        $childArray['visible'] = $child->visible;
        return $childArray;
    }

    /**
     * This menu will generate menu for main user site.
     */
    public function generateMenuForMainSite() {
        $menuArray = $this->getMenuArray("visibled");
        $finalMenu = "";
        $allParents = $this->getAllParents($menuArray);
        $sizeofParents = sizeof($allParents);
        $menuIndex = 0;
        foreach ($menuArray as $menu) {
            $menuIndex++;
            $hasChild = sizeof($menu['child']) > 0 ? true : false;
            if (!$hasChild) {
                $finalMenu .= $this->generateOnlySingleLineMenu($menu, $hasChild);
            } else {
                $strplace = str_replace(" ", "-", $menu['parent']['name']);
                $localMenu = "<li class='dropdown'  key='" . $strplace . "'><a href='#' class='dropdown-toggle' data-toggle='dropdown'> " . $menu['parent']['name'] . " <b class='caret'></b></a>";
                $localMenu .= "<ul class='dropdown-menu'>";
                #child menu start
                $childMenu = $menu['child'];
                foreach ($childMenu as $child) {
                    $localMenu .= $this->generateOnlySingleLineMenu($child, $hasChild);
                }
                #child menu complete
                $localMenu .="</ul></li>";
                $finalMenu .=$localMenu;
            }
            if ($menuIndex < $sizeofParents) {
                $finalMenu .= "<li class='divider-vertical'></li>";
            }
        }
        return $finalMenu;
    }

    /**
     * 
     * @param type $menu
     */
    public function generateOnlySingleLineMenu($menu, $hasChild) {
        $gen = $this->generateCredentials($menu, $hasChild);
        $strplace = str_replace(" ", "-", $gen['name']);
        $menuString = "<li  key='" . $strplace . "'><a href='" . $gen['url'] . "'> " . $gen['name'] . "</a></li>";
        return $menuString;
    }

    /**
     * 
     * @param type $menu
     * @return string
     */
    public function generateCredentials($menu, $hasChild) {
        $menuName = "";
        $menuId = "";
        if ($hasChild) {
            $menuName = $menu['name'];
            $menuId = $menu['id'];
        } else {
            $menuName = $menu['parent']['name'];
            $menuId = $menu['parent']['id'];
        }

        $strplace = str_replace(" ", "-", $menuName);
        $menuUrl = "http://" . $_SERVER['SERVER_NAME'] . "/menu/" . $menuId . "/" . $strplace;
        $array = array(
            'name' => $menuName,
            'url' => $menuUrl
        );
        return $array;
    }

    /**
     * 
     * @param type $menuArray
     */
    public function getAllParents($menuArray) {
        $parentMenuArray = array();
        foreach ($menuArray as $menu) {
            $localArray = array(
                'id' => $menu['parent']['id'],
                'name' => $menu['parent']['name']
            );
            array_push($parentMenuArray, $localArray);
        }
        return $parentMenuArray;
    }

}